package security.crypto;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import data.model.Data;

public class BlowFishCipher implements security.crypto.Cipher {

	
	
	@Override
	public Data encrypt(Data data,SecretKey skey)  {

        byte[] raw = skey.getEncoded();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");

        Cipher cipher;
			try {
				cipher = Cipher.getInstance("Blowfish");
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			byte[] encrypted = 
		    cipher.doFinal(data.getData());
			return new Data(encrypted);
			}
			catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null; 
			
		

	}
/*
 * http://tech.chitgoks.com/2009/07/15/encrypt-and-decrypt-using-blowfish-in-java/
 * (non-Javadoc)
 * @see security.crypto.Cipher#decrypt(data.model.Data, javax.crypto.SecretKey)
 */
	@Override
	public Data decrypt(Data data,SecretKey key) {
		try{
		Cipher cipher = Cipher.getInstance("Blowfish");
	     cipher.init(Cipher.DECRYPT_MODE, key);
	     return new Data(cipher.doFinal(data.getData()));
	  } catch (Exception e) { return null; }
	}

	

}
